﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Account_Registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        FormsAuthentication.SignOut();
        MasterPage master = (MasterPage)this.Master;
        Menu navMenu = (Menu)master.FindControl("NavigationMenu");
        navMenu.Visible = false;

        if (Session["LoginError"] != null)
        {
            otherErrors.Text = Session["LoginError"].ToString();
        }
    }

    /// <summary>
    /// Add cookies to response.
    /// </summary>
    /// <param name="login"></param>
    /// <param name="createPersCookie"></param>
    private void AddCookies(string user_id, bool createPersCookie)
    {
        HttpCookie authCookie = FormsAuthentication.GetAuthCookie(user_id, createPersCookie);
        FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
        FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, "userDataString");
        authCookie.Value = FormsAuthentication.Encrypt(newTicket);
        Response.Cookies.Add(authCookie);
    }
    
    /// <summary>
    /// Register user.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        MatrixData mdata = new MatrixData();
        if (LoginTextBox.Text.Length >= 50)
        {
            Session["LoginError"] = "Login is too long.";
            Response.Redirect("~/Account/Register.aspx");
        }
        DateTime birthday;
        if (!checkDate(DateBox.Text))
        {
            Session["LoginError"] = "Date is incorrect.";
            Response.Redirect("~/Account/Register.aspx");
        }
        birthday = DateTime.ParseExact(DateBox.Text.Substring(0, 2) +
            DateBox.Text.Substring(3, 2) + DateBox.Text.Substring(6, 4), "ddMMyyyy",
                System.Globalization.CultureInfo.CurrentCulture);
 
        if (mdata.getUserIdFromLogin(LoginTextBox.Text) == -1)
        {
            if (FotoUpload.HasFile)
            {
                mdata.addUserWithFoto(NameTextBox.Text, SurnameTextBox.Text,
                    birthday, CityTextBox.Text,
                        LoginTextBox.Text, PassTextBox.Text, FotoUpload.FileBytes);
                string user_id = mdata.getUserIdFromLogin(LoginTextBox.Text) + "";
                AddCookies(user_id, true);
            }
            else
            {
                mdata.addUser(NameTextBox.Text, SurnameTextBox.Text,
                    birthday, CityTextBox.Text,
                        LoginTextBox.Text, PassTextBox.Text);
                string user_id = mdata.getUserIdFromLogin(LoginTextBox.Text) + "";
                AddCookies(user_id, true);
            }
            Session["LoginError"] = null;
            Response.Redirect("~/messages.aspx");
        }
        else
        {
            Session["LoginError"] = "Login already used.";
            Response.Redirect("~/Account/Register.aspx");
        }


    }

    private bool checkDate(string date)
    {
        string[] parts = date.Split('.');
        bool vis = false;
        if (Int32.Parse(parts[2]) % 4 == 0)
        {
            vis = true;
        }

        switch (Int32.Parse(parts[1]))
        {
            case 2: 
            {
                if (vis)
                {
                    if (Int32.Parse(parts[0]) > 29)
                        return false;
                }
                else 
                {
                    if (Int32.Parse(parts[0]) > 28)
                        return false;
                }
                break; 
            }
            case 4: 
            {
                if (Int32.Parse(parts[0]) > 30)
                    return false; 
                break; 
            }
            case 6:
            {
                if (Int32.Parse(parts[0]) > 30)
                    return false;
                break;
            }
            case 9:
            {
                if (Int32.Parse(parts[0]) > 30)
                    return false;
                break;
            }
            case 11:
            {
                if (Int32.Parse(parts[0]) > 30)
                    return false;
                break;
            }
            default:
            {
                if (Int32.Parse(parts[0]) > 31)
                    return false;
                break;
            }
        }
        return true;
    }

    
}